<template>
  <basic-container>
    <avue-crud
      :option="option"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      :table-loading="loading"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @selection-change="selectionChange"
      row-key="onlyId"
    >
      <template slot="menu" slot-scope="{ row }">
        <el-button @click="detailEvent(row)" type="text" size="small" icon="el-icon-view" v-if="permission.cloudMaterial_view">详 情</el-button>
      </template>
    </avue-crud>
    <el-dialog append-to-body :visible.sync="flagDialog" title="详情">
      <avue-crud ref="crud" :option="optionDialog" :data="dataDialog" :table-loading="packDetailLoading">
        <template slot-scope="{ row }" slot="packStatus">
          <span>
            {{ row.packStatus | statusConvert(val) }}
          </span>
        </template>
      </avue-crud>
    </el-dialog>
  </basic-container>
</template>

<script>
//    <!-- :table-loading="loading"
import { getList, getDetail, add, update, remove } from '@/api/cloud/cloud_material';
import { mapGetters } from 'vuex';

export default {
  name: 'CloudCloudMaterial',
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      packDetailLoading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      params: {},
      selectionList: [],
      flagDialog: false,
      StockLabel: '',
      options: [],
      option: {
        height: 'calc(100vh - 440px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 4,
        refreshBtn: true, //刷新按钮是否显示
        columnBtn: false, //控制表格列是否显示按钮是否显示7
        searchBtn: true, // 显示查询的按钮
        searchSpan: 5,
        border: true,
        index: true,
        viewBtn: true,
        dialogClickModal: false,
        addBtn: false,
        delBtn: false,
        editBtn: false,
        viewBtn: false,
        width: '100',
        searchLabelWidth: 100,
        searchMenuPosition: 'right',
        searchShow: true,
        indexLabel: '序号',
        column: [
          {
            label: '库存中心',
            prop: 'inventoryName'
          },
          {
            label: '库存中心',
            prop: 'inventoryId',
            search: true,
            searchOrder: 1,
            type: 'select',
            hide: true,
            dicUrl: '/api/mes-admin/inventory/list',
            props: {
              label: 'name',
              value: 'id'
            },
            cascader: ['areaId']
          },
          {
            label: '储位编码',
            prop: 'areaCode'
          },
          {
            label: '储位名称',
            prop: 'areaId',
            type: 'select',
            // searchType: "search",
            search: true,
            hide: true,
            dicUrl: '/api/mes-admin/area/select/inventory?inventoryId={{key}}',
            props: {
              label: 'name',
              value: 'id'
            },
            dicFlag: false
          },
          {
            label: '储位名称',
            prop: 'areaName'
          },
          {
            label: '物料编码',
            prop: 'partMasterCode',
            search: true,
            span: 4
          },
          {
            label: '物料名称',
            prop: 'partMasterName'
          },
          {
            label: '物料批次',
            prop: 'lotNo',
            search: true
          },
          {
            label: '物料数量',
            prop: 'quantity'
          },
          {
            label: '包装数',
            prop: 'packs'
          }
          //   {
          //     label: '入库时间',
          //     prop: 'createTime',
          //     type: 'datetime',
          //     format: 'yyyy-MM-dd hh:mm',
          //     valueFormat: 'yyyy-MM-dd hh:mm',
          //     hide: true,
          //     search: true
          //   }
          // {
          //   label: '供应商编码',
          //   prop: 'vendorCode',
          //   search: true,
          //   // searchLabelWidth: 90,
          //   span: 4
          //   // width: 100
          // },
          // {
          //   label: '供应商名称',
          //   prop: 'vendorName',
          //   // search: true,
          //   // searchLabelWidth: 90,
          //   span: 4
          // }
        ]
      },
      optionDialog: {
        height: 'calc(100vh - 400px)',
        calcHeight: 30,
        delBtn: false,
        editBtn: false,
        addBtn: false,
        refreshBtn: false, //刷新按钮是否显示
        columnBtn: false, //控制表格列是否显示按钮是否显示
        border: true,
        menu: false, //表格操作栏是否显示
        column: [
          {
            label: '包装条码',
            prop: 'sequenceNumber'
          },
          {
            label: '包装数量',
            prop: 'packQuantity',
            span: 6
          },
          {
            label: '物料批次',
            prop: 'lotNo',
            span: 6
          },
          {
            label: '供应商编码',
            prop: 'vendorCode',
            span: 6
          },
          {
            label: '供应商名称',
            prop: 'vendorName'
          },
          {
            label: '入库时间',
            prop: 'createTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            searchRange: true,
            span: 4
          },
          {
            label: '状态',
            prop: 'packStatus',
            searchLabelWidth: 90,
            span: 4
          }
        ]
      },
      data: [],
      dataDialog: []
    };
  },
  filters: {
    statusConvert(val) {
      switch (val) {
        case 1:
          return '正常';
        case 2:
          return '全部锁定';
        case 3:
          return '部分锁定';
        case 4:
          return '报废';
        case 5:
          return '已出库';
        case 6:
          return '已入库';
        default:
          break;
      }
    }
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.cloudMaterial_view, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    detailEvent(row) {
      console.log(row, row.id, 'rowrow');
      this.flagDialog = true;
      getDetail(row.id).then((res) => {
        this.packDetailLoading = true;
        if (res.data.data) {
          this.dataDialog = res.data.data;
          this.packDetailLoading = false;
        }
      });
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          console.log(row, 'rwoew');
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset(params, done) {
      this.query = {};
      console.log(this.page.currentChange, 'this.page.currentChangethis.page.currentChange');
      this.page.currentPage = 1;
      this.page.pageSize = 15;
      this.onLoad(this.page, this.query);
    },
    searchChange(params, done) {
      if (params.createTime) {
        params.startDate = params.createTime[0];
        params.endDate = params.createTime[1];
        delete query.createTime;
      }

      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, this.query);

      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page, this.params);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    refreshChange() {
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data ? (data.total ? data.total : '') : '';
        const tableData = (data.records || []).map((item, index) => ({
          ...item,
          areaname: item.areaName,
          onlyId: item.id + index
          // id: item.id + index
        }));
        this.data = tableData || [];
        this.loading = false;
      });
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
    this.onLoad(this.page);
  }
};
</script>

<style lang="scss" scoped></style>
